package dao

import (
	"context"
	"gitee.com/chunanyong/zorm"
	"gitee.com/xinshortlink/internal/dal/db/model"
	"gitee.com/xinshortlink/internal/dto"
	"go.uber.org/zap"
)

type VisitRecordDao struct {
	logger *zap.Logger
}

func NewVisitRecordDao(logger *zap.Logger) *VisitRecordDao {
	return &VisitRecordDao{
		logger: logger,
	}
}

func (vrd *VisitRecordDao) Create(ctx context.Context, vr *model.VisitRecordStruct) error {
	_, err := zorm.Insert(ctx, vr)
	return err
}

func (vrd *VisitRecordDao) CreateBatch(ctx context.Context, vrs []*model.VisitRecordStruct) error {

	s := make([]zorm.IEntityStruct, len(vrs))
	for i, v := range vrs {
		s[i] = v
	}

	_, err := zorm.InsertSlice(ctx, s)
	return err
}

func (vrd *VisitRecordDao) FindVisitRecord(ctx context.Context, req *dto.VisitRecordReq) (*zorm.Page, []*model.VisitRecordStruct, error) {
	finder := zorm.NewSelectFinder(model.VisitRecordStructTableName).
		Append("WHERE short_id = ? AND visit_time BETWEEN ? AND ?", req.ShortId, req.StartTime, req.EndTime)

	page := zorm.NewPage()
	page.PageNo = req.PageNo
	page.PageSize = req.PageSize

	list := make([]*model.VisitRecordStruct, 0)
	err := zorm.Query(ctx, finder, &list, page)
	return page, list, err
}
